//
//  CrystalMinds SQLiteDatabase Library
//  Version 2.3
//
//  Created by Mark van Wijnen on 16-10-09.
//  Last updated on 26-10-09.
//  Copyright 2009 CrystalMinds. All rights reserved.
//  Mail: info@crystalminds.nl
//
// This work is licensed under the Creative Commons Attribution-Noncommercial- 
// No Derivative Works 3.0 Unported License.
// To view this license go to http://creativecommons.org/licenses/by-nc-nd/3.0/ 
// or send a letter to Creative Commons, 171 Second Street, Suite 300, 
// San Francisco, California, 94105, VS.

#import <Foundation/Foundation.h>
#import <UIKit/UIKit.h>
#import <sqlite3.h>

// Datatypes In SQLite Version 3
#define CMSQLiteDatabaseDataTypeNull    @"NULL"
#define CMSQLiteDatabaseDataTypeInteger @"INTEGER"
#define CMSQLiteDatabaseDataTypeFloat   @"REAL"
#define CMSQLiteDatabaseDataTypeString  @"TEXT"
#define CMSQLiteDatabaseDataTypeObject  @"BLOB"

@interface CMSQLiteDatabase : NSObject {
@private
	NSError *_error;
	NSString *_filenameOfDatabase;
	NSString *_pathToDatabase;
	sqlite3 *_databaseHandle;
	NSMutableDictionary *_cachedStatements;
	
@public
	NSInteger lastInsertRowId;
	NSString *lastQuery;
	NSInteger maxAmountOfCachedStatements;
}

@property (nonatomic, readonly) NSInteger lastInsertRowId;
@property (nonatomic, readonly) NSString *lastQuery;
@property (nonatomic) NSInteger maxAmountOfCachedStatements;

+ (CMSQLiteDatabase *)sharedDatabase;

- (id)initWithFilename:(NSString *)filename;

- (NSArray *)allColumnsForTable:(NSString *)tableName;
- (NSArray *)allColumnsSchemaForTable:(NSString *)tableName;
- (NSArray *)allTables;
- (BOOL)columnExists:(NSString *)columnName inTable:(NSString *)tableName;
- (NSInteger)clearCachedStatements;
- (NSInteger)clearDatabase;
- (NSInteger)clearTable:(NSString *)tableName;
- (NSInteger)deleteColumn:(NSString *)columnName inTable:(NSString *)tableName;
- (NSInteger)deleteRowsinTable:(NSString *)table;
- (NSInteger)deleteRowWhereColumn:(NSString *)columnName isEqualTo:(id)object inTable:(NSString *)tableName;
- (NSInteger)deleteTable:(NSString *)tableName;
- (NSString *)description;
- (NSError *)error;
- (NSError *)errorFromCode:(NSInteger)errorCode;
- (NSInteger)insertColumns:(NSDictionary *)columns intoTable:(NSString *)tableName;
- (NSInteger)insertRow:(NSDictionary *)row intoTable:(NSString *)tableName;
- (NSInteger)insertTable:(NSString *)tableName;
- (NSInteger)insertTable:(NSString *)tableName withColumns:(NSDictionary *)columns;
- (NSInteger)renameTable:(NSString *)tableName to:(NSString *)newTableName;
- (NSArray *)resultsForQuery:(NSString *)sql;
- (NSArray *)resultsForQuery:(NSString *)sql arguments:(NSArray *)arguments;
- (NSDictionary *)rowForQuery:(NSString *)sql rowOffset:(NSInteger)rowOffset;
- (NSDictionary *)rowForQuery:(NSString *)sql arguments:(NSArray *)arguments rowOffset:(NSInteger)rowOffset;
- (NSArray *)rowsOfTable:(NSString *)tableName;
- (BOOL)tableExists:(NSString *)tableName;
- (NSInteger)query:(NSString *)sql;
- (NSInteger)query:(NSString *)sql arguments:(NSArray *)arguments;
- (NSInteger)updateRow:(NSDictionary *)row inTable:(NSString *)tableName autoInsert:(BOOL)autoInsert;
- (NSInteger)updateRow:(NSDictionary *)row whereColumn:(NSString *)column isEqualTo:(id)object inTable:(NSString *)tableName autoInsert:(BOOL)autoInsert;
- (id)variableForQuery:(NSString *)sql column:(NSString *)columnName rowOffset:(NSInteger)rowOffset;
- (id)variableForQuery:(NSString *)sql arguments:(NSArray *)arguments column:(NSString *)columnName rowOffset:(NSInteger)rowOffset;

@end
